#!perl -w

use Cwd;

my $CUR_HOME= cwd;

my $DEV_HOME="..\\..\\..";
my $HOME="\"C:\\Program Files\\Microsoft Visual Studio .NET 2003\"\\Vc7\\bin";

my $MAKE=$HOME."\\nmake";

my $IMIBASE_HOME=$DEV_HOME."\\imiBase\\project\\mtk\\project";
my $MIDDLEWARE_HOME=$DEV_HOME."\\imiBase\\middleware\\project\\embed";
my $UI_HOME=$DEV_HOME."\\imeworks\\uiengine\\project\\embed";
my $BILL_HOME=$DEV_HOME."\\imeworks\\billengine\\common\\project\\embed";
my $APPMANAGER_HOME=$DEV_HOME."\\imiBase\\AppManager\\project\\embed";

my $error = 0;

print "\n UI engine begin \n";
#ui
chdir $CUR_HOME;
chdir $UI_HOME or die "$!\n";
do_make("makefile_arm7.mak", "release");
do_make("makefile_arm7.mak", "debug");

print "\n AppManager begin \n";
#AppManager
chdir $CUR_HOME;
chdir $APPMANAGER_HOME or die "$!\n";
do_make("makefile_arm7.mak", "release");
do_make("makefile_arm7.mak", "debug");

print "\n bill begin \n";
#bill
chdir $CUR_HOME;
chdir $BILL_HOME or die "$!\n";
do_make("makefile_arm7.mak", "release");
do_make("makefile_arm7.mak", "debug");

print "\n middleware begin \n";
#middleware
chdir $CUR_HOME;
chdir $MIDDLEWARE_HOME or die "$!\n";
do_make("makefile_arm7.mak", "release");
do_make("makefile_arm7.mak", "debug");

print "\n imibase begin \n";
#imibase
chdir $CUR_HOME;
chdir $IMIBASE_HOME or die "$!\n";
do_make("makefile_arm7.mak", "release");
do_make("makefile_arm7.mak", "debug");

print "\nrelease ok\n";
exit;

#input[0]: makefile
#input[1]: flag(debug, release)
sub do_make
{
	my $makefile = $_[0];
	my $flag = $_[1];
	
	system($MAKE." /f $makefile RELEASE_FLAG=$flag 1>a.txt 2>b.txt");
	my $dir = cwd();
	
	if (parse_file("a.txt"))
	{
		die "\n$dir error\n";
	}
	
	if (parse_file("b.txt"))
	{
		die "\n$dir error\n";
	}
}

sub parse_file
{
  my $file = $_[0];
    
	my($ok) = open my $fp, "< $file";
	if (!$ok)
	{
		die "ERROR: can't read $file\n$!";
	}
	else
	{
		while (<$fp>)
		{
		    if (/fatal error/i)
		    {
		    	$error = 1;
		    }
		}
		close $fp;
	}
	
	$error = $error;
}